/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.loja.view.component.composition;

import br.com.loja.view.component.factory.ViewHadler;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author DavidLuiz
 */
public class CalendarToOne extends JFormattedTextField implements ViewHadler<Calendar> {

    public CalendarToOne() {
        init();
    }

    private void init() {
        try {
            MaskFormatter dataFormatter;
            dataFormatter = new MaskFormatter("##/##/####");
            dataFormatter.setValidCharacters("0123456789");
            setFormatter(dataFormatter);

        } catch (ParseException ex) {
            System.err.println(ex.getMessage());
            ex.printStackTrace();
        }
    }

    @Override
    public void setValueModel(Calendar value) {

        setText("");

        if (value != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            String text = dateFormat.format(((Calendar) value).getTime());
            setValue(text);
        }

    }

    @Override
    public Calendar getValueModel() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
